package com.heima.article.service.Impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ApArticleService;
import com.heima.article.service.ArticleFreeMarkerService;
import com.heima.common.constants.ArticleConstants;
import com.heima.common.constants.WemediaConstants;
import com.heima.common.redis.CacheService;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.pojo.ApArticle;
import com.heima.model.article.pojo.ApArticleConfig;
import com.heima.model.article.pojo.ApArticleContent;
import com.heima.model.article.vos.HotArticleVo;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service
@Slf4j
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {

    private final static short MAX_PAGE_SIZE = 50;


    @Autowired
    private ApArticleMapper apArticleMapper;

    @Override
    public ResponseResult load(ArticleHomeDto dto, Short type) {
        Integer size = dto.getSize();
        if (size==null){
            size = 10;
        }

        size = Math.min(size,MAX_PAGE_SIZE);

        if (!type.equals(ArticleConstants.LOADTYPE_LOAD_MORE)&&!type.equals(ArticleConstants.LOADTYPE_LOAD_NEW)){
            type=1;
        }

        if (StringUtils.isBlank(dto.getTag())){
            dto.setTag(ArticleConstants.DEFAULT_TAG);
        }


        if(dto.getMaxBehotTime()==null)dto.setMaxBehotTime(new Date());
        if(dto.getMinBehotTime()==null)dto.setMinBehotTime(new Date());


        List<ApArticle> apArticles = apArticleMapper.loadArticleList(dto, type);

        return ResponseResult.okResult(apArticles);
    }


    @Autowired
    private CacheService cacheService;
    @Override
    public ResponseResult load2(ArticleHomeDto dto, Short type, boolean firstPage) {
        if (firstPage){

            String s = cacheService.get(ArticleConstants.HOT_ARTICLE_FIRST_PAGE + dto.getTag());
            if (StringUtils.isNotBlank(s)){
                List<HotArticleVo> hotArticleVos = JSON.parseArray(s, HotArticleVo.class);
                ResponseResult responseResult = ResponseResult.okResult(hotArticleVos);
                return responseResult;
            }
        }

        return load(dto,type);
    }

    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;

    @Autowired
    private ApArticleContentMapper apArticleContentMapper;

    @Autowired
    private ArticleFreeMarkerService articleFreeMarkerService;

    public ResponseResult saveArticle(ArticleDto dto) {
//
//        if (dto==null){
//            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
//        }
//
//        //判断是否存在id
//
//        ApArticle apArticle = new ApArticle();
//        BeanUtils.copyProperties(dto,apArticle);
//        if(dto.getId()==null){
//
//            save(apArticle);
//            ApArticleConfig apArticleConfig = new ApArticleConfig(apArticle.getId());
//            apArticleConfigMapper.insert(apArticleConfig);
//
//            ApArticleContent apArticleContent = new ApArticleContent();
//            apArticleContent.setArticleId(apArticle.getId());
//            apArticleContentMapper.insert(apArticleContent);
//        }else {
//            updateById(apArticle);
//
//            ApArticleContent apArticleContent = apArticleContentMapper.selectOne(Wrappers.<ApArticleContent>lambdaQuery().eq(ApArticleContent::getArticleId, apArticle.getId()));
//            apArticleContent.setContent(dto.getContent());
//            apArticleContentMapper.updateById(apArticleContent);
//
//
//        }
//
//        return ResponseResult.okResult(apArticle.getId());

        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        ApArticle apArticle = new ApArticle();
        BeanUtils.copyProperties(dto,apArticle);

        if(dto.getId()==null){
            save(apArticle);
            ApArticleConfig apArticleConfig = new ApArticleConfig(apArticle.getId());
            apArticleConfigMapper.insert(apArticleConfig);
            ApArticleContent apArticleContent = new ApArticleContent();
            apArticleContent.setContent(dto.getContent());
            apArticleContentMapper.insert(apArticleContent);
        }else{
            updateById(apArticle);
            ApArticleContent apArticleContent = apArticleContentMapper.selectOne(Wrappers.<ApArticleContent>lambdaQuery().eq(ApArticleContent::getArticleId, apArticle.getId()));
            apArticleContentMapper.updateById(apArticleContent);
        }

        articleFreeMarkerService.buildArticleMinIo(apArticle,dto.getContent());



        return ResponseResult.okResult(apArticle.getId());

    }
}
